/*
 * TODO
 */
package com.xtrip.adm.stat.data;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;

import com.xtrip.adm.model.CBD;
import com.xtrip.adm.model.City;
import com.xtrip.adm.model.Province;
import com.xtrip.common.BasicConstant;
import com.xtrip.util.IdGenerator;
import com.xtrip.util.parse.xml.XtripDocumentHelper;

/**
 * @project xtrip
 * @author shiming.yao
 * @time 2013-6-5
 */
/*
 * 1.1.1	酒店省 城市 行政区 商业区
 * hotel-goe-cn.xml
 * */
public class TcHotelGeo {

	private List<City> cList = null;
	private List<Province> pList = null;
	private List<CBD> cbdList = null;
	/**
	 * 
	 */
	public TcHotelGeo() {
		cList = new ArrayList<City>();
		pList = new ArrayList<Province>();
		cbdList = new ArrayList<CBD>();
	}
	public void parse(String file){
		try {
			Document doc = XtripDocumentHelper.readFile(file);
			Element root = doc.getRootElement();
			
			List<Element> provs = root.elements();
			for(Element prov : provs){
				Province p = new Province();
				List<Element> pinfos = prov.elements();
				for(Element pinfo : pinfos){
					String  name = pinfo.getName();
					String value = pinfo.getStringValue();
					if(name.equals("id")){
						p.setId(value);
					} else if(name.equals("name")){
						p.setName(value);
					} else if(name.equals("pinyin")){
						p.setPinyin(value);
					} else if(name.equals("index")){
						p.setIndex(value);
					} else if(name.equals("cityList")){
						List<Element> citys = pinfo.elements();
						for(Element city : citys){
							List<Element> citems = city.elements();
							City c = new City();
							c.setProId(p.getId());
							c.setId(IdGenerator.generateId(BasicConstant.PREFIX_CITY));
							for(Element citem : citems){
								String  cname = citem.getName();
								String cvalue = citem.getStringValue();
								if(cname.equals("id")){
									c.setTcCid(cvalue);
								} else if(cname.equals("name")){
									c.setName(cvalue);
								} else if(cname.equals("pinyin")){
									c.setPinyin(cvalue);
								} else if(cname.equals("index")){
									c.setAbcd(cvalue);
								} else if(cname.equals("sectionList")){
									List<Element> sections = citem.elements();
									for(Element section : sections){
										Element idEl = section.element("id");
										Element nameEl = section.element("name");
										CBD cbd = new CBD();
										cbd.setId(IdGenerator.generateId(BasicConstant.PREFIX_CBD));
										cbd.setCityId(c.getId());
										cbd.setTcCBDId(idEl.getStringValue());
										cbd.setName(nameEl.getStringValue());
										cbdList.add(cbd);
									}
								}
							}
							cList.add(c);
						}
					}
				}
				pList.add(p);
			}
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public List<City> getcList() {
		return cList;
	}

	public void setcList(List<City> cList) {
		this.cList = cList;
	}

	public List<Province> getpList() {
		return pList;
	}

	public void setpList(List<Province> pList) {
		this.pList = pList;
	}

	public List<CBD> getCbdList() {
		return cbdList;
	}

	public void setCbdList(List<CBD> cbdList) {
		this.cbdList = cbdList;
	}
}
